package com.sgb.knowledgeshare;

import android.text.TextUtils;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import com.github.dfqin.grantor.PermissionListener;
import com.github.dfqin.grantor.PermissionsUtil;
import com.sgb.knowledgeshare.databinding.ActivityMainShareLoginBinding;
import com.sgb.knowledgeshare.utils.SharePermissionUtils;
import com.sgb.knowledgeshare.view.activity.HomeShareActivity;
import com.swgk.core.base.AppActivity;
import com.swgk.core.util.MToast;

public class ShareMainActivity extends AppActivity {

    public static final String[] BASIC_EXTERNAL_STORAGE = new String[]{
            android.Manifest.permission.CAMERA,
            android.Manifest.permission.RECORD_AUDIO,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
            android.Manifest.permission.READ_EXTERNAL_STORAGE
    };


    private ActivityMainShareLoginBinding mBinding;
    private shareMainModel sModel;

    @Override
    protected void initView() {
        super.initView();
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main_share_login);
        sModel = new ViewModelProvider(this).get(shareMainModel.class);
        sModel.setActivirt(this);
        mBinding.editPhone.setText("18229065220");
        mBinding.editPws.setText("qwe111");
        mBinding.login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = mBinding.editPhone.getText().toString();
                String pws = mBinding.editPws.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    MToast.showToast(ShareMainActivity.this, "请输入账号");
                    return;
                }
                if (TextUtils.isEmpty(pws)) {
                    MToast.showToast(ShareMainActivity.this, "请输入密码");
                    return;
                }
                sModel.pwdLoginModel(phone, pws);
            }
        });
        checkingPermission();
    }

    /**
     * 判断权限是否授权
     */
    private void checkingPermission() {
        if (PermissionsUtil.hasPermission(getActivity(), BASIC_EXTERNAL_STORAGE)) {

        } else {
            PermissionsUtil.requestPermission(this, new PermissionListener() {
                @Override
                public void permissionGranted(@NonNull String[] permissions) {

                }

                @Override
                public void permissionDenied(@NonNull String[] permissions) {
                    checkingPermission();
                }
            }, BASIC_EXTERNAL_STORAGE);
        }
    }

    public void startHomeShare() {
        SharePermissionUtils.getPermission(this);
    }
}